/**
 * Classname : SchedulerFactory.java
 *
 * Version information: 1.0
 *
 * Date: Mar 09, 2012 Bui.Trung.Dung create
 *
 */
package com.history.scheduler;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.history.common.Logit;

public class SchedulerFactory {

	private static Logit log = Logit.getInstance(SchedulerFactory.class);

	private static Map<String, SchedulerThread> schedulerThreads = Collections
			.synchronizedMap(new HashMap<String, SchedulerThread>());
	
	static SchedulerFactory schedulerFactory = new SchedulerFactory();

	private SchedulerFactory() {
		init();
	}

	/**
	 * @return
	 */
	public static SchedulerFactory getInstance() {
		return schedulerFactory;
	}

	/**
	 * initializes scheduler for all symbol name
	 */
	private void init() {

		try {
			// CoverNetworkProblem
			schedulerThreads.put("CoverNetworkProblem", new SchedulerThread());
			// SynchronizeFromServer
			new SysSchedulerThread();

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * @param currencyPair
	 */
	public void cancelScheduler(String symbolName) {
		try {

		} catch (Exception e) {
			log.error("Cancel Scheduler error" + symbolName);
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * @param currencyPair
	 */
	public void startScheduler(String symbolName) {
		try {


		} catch (Exception e) {
			log.error("Start Scheduler error" + symbolName);
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * close the threads of particular currency pair
	 */
	public void closeSchedulerThreads() {
		try {
			Iterator<Entry<String, SchedulerThread>> iterator = schedulerThreads
					.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, SchedulerThread> obj = iterator.next();
				obj.getValue().close();
				obj.setValue(null);

			}

		} catch (Exception e) {
			log.error("Could Not close the scheduler threads" + e.getMessage(),
					e);
		}
	}

}
